package com.itheima.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import com.itheima.dto.*;
import com.itheima.entity.Houses;
import com.itheima.mapper.HousesMapper;
import com.itheima.service.IHousesService;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.wltea.analyzer.core.IKSegmenter;
import org.wltea.analyzer.core.Lexeme;
import org.wltea.analyzer.lucene.IKAnalyzer;

import java.io.IOException;
import java.io.StringReader;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
* houses 相关service实现类
* @author WEI
* @since 2021-8-6 15:38:56
*/
@Service
public class HousesServiceImpl  implements IHousesService {

@Autowired
private HousesMapper housesMapper;

    @Override
    public List<HouseDTO> houseList(String areaId) {
           return  housesMapper.houseList(areaId);
    }


    @Override
    public String selectCityById(int id){
        QueryWrapper<Houses> queryWrapper  = new QueryWrapper<>();
          queryWrapper.eq("id",id);
        Houses houses =  housesMapper.selectOne(queryWrapper);
        String s  = houses.getAreaName();
        String[] strings = s.split("\\|");
        return strings[0];
    }

    @Override
    public HouseDetailDTO selectHouseDetailById(HouseRequest houseRequest) {
        HouseDetail houseDetail =  housesMapper.selectHouseDetailById(houseRequest.getHouseId());
        HouseDetailDTO houseDetailDTO  = new HouseDetailDTO();
        houseDetailDTO.setDetail(houseDetail);
        Map<String,Object> map = new HashMap<>();
        map.put("price",houseDetail.getPriceNum());
        map.put("areaName",houseRequest.getAreaName());
        map.put("excludeId",houseRequest.getHouseId());
        List<HouseDTO> houseDTOS = housesMapper.selectLikes(map);
        houseDetailDTO.setLikeList(houseDTOS);
        return houseDetailDTO;


    }

    @Override
    public  IPage<HouseDTO> selectHouseByFileter(HouseFilter houseFilter,int i) throws IOException {
        //是mp
        Map<String,Object> map = new HashMap<>();
        if(houseFilter.getKeywords()==null){
            map.put("keywords",null);
        }else {
            StringReader sr = new StringReader(houseFilter.getKeywords());
            IKSegmenter ik = new IKSegmenter(sr, true);
            Lexeme lex = null;
            List<String> res = new ArrayList<>();
            while ((lex = ik.next()) != null) {
                res.add(lex.getLexemeText());
            }

            map.put("keywords", res);
        }
        map.put("distinct", houseFilter.getDistinct());
        map.put("subway",houseFilter.getSubway());
        map.put("payway",houseFilter.getPayway());
        map.put("money",houseFilter.getMoney());
        map.put("direction",houseFilter.getDirection()) ;
        map.put("floor",houseFilter.getFloor());
        map.put("tags",houseFilter.getTags());
        Page<HouseDTO> page = new Page<>(1,i);
        IPage<HouseDTO> all= housesMapper.selectByFilter(page, map);
        return all;
       //使用分页插件进行分页

    }

}